package com.qidian.tcba.admin.service.impl;

import com.qidian.tcba.admin.model.dto.user.*;
import com.qidian.tcba.admin.model.query.UserQueryDTO;
import com.qidian.tcba.admin.model.entity.District;
import com.qidian.tcba.admin.model.entity.User;
import com.qidian.tcba.admin.service.UserService;
import com.qidian.tcba.dao.DistrictMapper;
import com.qidian.tcba.dao.UserMapper;
import com.qidian.tcba.utils.BeanMapper;
import com.qidian.tcba.utils.MessageEncrypt;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 冯永龙
 * @since 2021-07-06
 */
@Service
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    private final DistrictMapper districtMapper;

    public UserServiceImpl(DistrictMapper districtMapper, UserMapper userMapper) {
        this.districtMapper = districtMapper;
        this.userMapper = userMapper;
    }

    @Override
    public boolean updateUserPassword(UpdateUserPasswordDTO updateUserPassword) {
        User user = userMapper.selectByPrimaryKey(updateUserPassword.getId());
        String oldPassword = MessageEncrypt.encrypt(updateUserPassword.getOldPassword(), null);
        assert oldPassword != null;
        if (oldPassword.equals(user.getPassword())){
            String newPassword = MessageEncrypt.encrypt(updateUserPassword.getNewPassword(), null);
            user.setPassword(newPassword);
            userMapper.updateByPrimaryKey(user);
            return true;
        }
        return false;
    }

    @Override
    public List<UserResultListDTO> getUserResultList(UserQueryDTO userQueryDTO) {
        List<User> users = userMapper.getUsers(userQueryDTO);
        return BeanMapper.mapList(users, UserResultListDTO.class);
    }

    @Override
    public boolean addUser(AddUserDTO addUserDTO) {
        User user = BeanMapper.map(addUserDTO, User.class);
        user.setStatus(1);
        District district = districtMapper.selectByPrimaryKey(addUserDTO.getDistrictId());
        if (district != null){
            user.setDistrictCode(district.getCode());
        }
        return userMapper.insert(user) == 1;
    }

    @Override
    public boolean updateUserStatus(int userId, int status) {
        return userMapper.updateStatusById(userId, status) == 1;
    }

    @Override
    public UserDTO login(String username, String password) {
        User user = userMapper.login(username, MessageEncrypt.encrypt(password, null));
        if (user != null){
            return BeanMapper.map(user,UserDTO.class);
        }
        return null;
    }

    @Override
    public boolean register(RegisterDTO registerDTO) {
        User user = userMapper.findByUsername(registerDTO.getUsername());
        if (user != null){
            return false;
        }else {
            String password = MessageEncrypt.encrypt(registerDTO.getPassword(), null);
            registerDTO.setPassword(password);
            userMapper.register(registerDTO);
            return true;
        }
    }
}
